package com.zr.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;

import com.zr.dao.RoleDao;
import com.zr.dao.impl.RoleDaoImpl;
import com.zr.model.Role;
import com.zr.service.RoleService;



/**
 * @Description
 * @author 郑少杰
 * @date 2017年9月13日 下午3:37:40
 * @version V1.3.1
 */

public class RoleServiceImpl implements RoleService {
    RoleDao roledao = new RoleDaoImpl();

    /**
     * Description 根据用户id插叙角色信息
     * 
     * @param userId 用户id
     * @return 返回角色信息
     * @see com.zr.service.UserService#searchByUserId(int)
     */

    @Override
    public Role searchByUserId(int userId) {
        // TODO Auto-generated method stub
        Role role = roledao.getRoleByUserId(userId);
        return role;
    }

    /**
     * Description 根据父节点和角色的功能列表来过滤功能
     * 
     * @param parentId 父节点
     * @param fids 功能列表
     * @return 功能列表的json数组
     * @see com.zr.service.RoleService#getFuncsByParentId(int, java.lang.String)
     */

    @Override
    public JSONArray getUserFuncs(int parentId, String fids) {
        // TODO Auto-generated method stub
        JSONArray jsonArray = roledao.getCurrentUserFuncsByParentId(parentId, fids);
        return jsonArray;
    }

    /**
     * Description 得到角色信息
     * 
     * @return 角色信息列表
     * @see com.zr.service.RoleService#getRoles()
     */

    @Override
    public List<Role> getRoles() {
        List<Role> list = roledao.selectRoles();
        return list;
    }

    /**
     * Description
     * 
     * @return
     * @see com.zr.service.RoleService#selectFids()
     */

    @Override
    public String selectFids() {
        List<Role> list = roledao.selectRoles();
        String fids = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                fids += String.valueOf(list.get(i).getRoleId());
            } else {
                fids += String.valueOf(list.get(i).getRoleId()) + ",";
            }
        }
        return fids;
    }

    /**
     * Description
     * 
     * @param page
     * @param pageSize
     * @return
     * @see com.zr.service.RoleService#getAllRoles(java.lang.String, java.lang.String)
     */

    @Override
    public List<Role> getAllRoles(String page, String pageSize) {
        List<Role> list = roledao.getAllRoles(Integer.parseInt(page), Integer.parseInt(pageSize));
        return list;
    }

    /**
     * Description
     * 
     * @param name
     * @param fids
     * @return
     * @see com.zr.service.RoleService#insertRoles(java.lang.String, java.lang.String)
     */

    @Override
    public boolean insertRoles(String name, String[] fids) {
        Set<Integer> set = new HashSet<>();
        for (String string : fids) {
            set.add(Integer.parseInt(string));
            set.addAll(roledao.getParents(Integer.parseInt(string)));
        }

        List<Integer> list = new ArrayList<>();
        list.addAll(set);
        String fString = "";
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == -1) {
                continue;
            }
            if (i == list.size() - 1) {
                fString += String.valueOf(list.get(i));
            } else {
                fString += String.valueOf(list.get(i)) + ",";
            }

        }
        return roledao.insertRole(name, fString);
    }

    /**
     * Description
     * 
     * @param roleId
     * @return
     * @see com.zr.service.RoleService#getFids(int)
     */

    @Override
    public Role getFids(int roleId) {

        return roledao.selectFids(roleId);
    }



}
